package com.junyidao.game.controller;

import com.junyidao.game.constant.BaseConstant;
import com.junyidao.game.constant.OperLogConstant;
import com.junyidao.game.constant.SysUserConstant;
import com.junyidao.game.core.LoginInfo;
import com.junyidao.game.core.annotation.Action;
import com.junyidao.game.pojo.SysPriv;
import com.junyidao.game.pojo.SysRole;
import com.junyidao.game.pojo.SysUser;
import com.junyidao.game.pojo.TChannel;
import com.junyidao.game.service.RoleService;
import com.junyidao.game.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * Created by zzh on 2016/8/27.
 */
@Controller
@RequestMapping("/sysUser")
public class SysUserController extends BaseController {


    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @RequestMapping
    @Action(desc = "进入账号管理页面")
    public ModelAndView list() {
        ModelAndView result = new ModelAndView("system/user_list");
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/list")
    @Action(desc = "获取账号管理列表", sysPriv = false)
    public Map<String, Object> listData(SysUser SysUser) {
        SysUser.setLevelCode(SysUserConstant.LEVEL_ONE);
        Map<String, Object> map = userService.getAll(SysUser);
        return resultSucess(map);
    }

    @ResponseBody
    @RequestMapping(value = "/activate")
    @Action(operType = OperLogConstant.USER_ACTIVATE, desc = "启用失效的账号", needLog = true)
    public Map<String, Object> activate(String uids) {
        userService.activate(uids, BaseConstant.STATUS_NORMAL);
        return resultSucess(null);
    }

    @ResponseBody
    @RequestMapping(value = "/disactivate")
    @Action(operType = OperLogConstant.USER_DISACTIVATE, desc = "注销启用的账号", needLog = true)
    public Map<String, Object> disactivate(String uids) {
        userService.activate(uids, BaseConstant.STATUS_DELETE);
        return resultSucess(null);
    }

    @RequestMapping(value = "/toAdd")
    @Action(desc = "进入新增账号页面")
    public ModelAndView toAdd() {
        ModelAndView result = new ModelAndView("system/user_add");
        List<SysRole> list = roleService.getAll();
        result.addObject("roleList", list);
        return result;
    }

//    @ResponseBody
//    @RequestMapping(value = "/roleList")
//    @Action(desc = "获取权限列表", sysPriv = false)
//    public Map<String, Object> privList(Integer uid) {
//        Map<String, Object> map = new HashMap<>();
//
//        return resultSucess(map);
//    }

    @ResponseBody
    @RequestMapping(value = "/add")
    @Action(operType = OperLogConstant.USER_ADD, desc = "新增账号", needLog = true)
    public Map<String, Object> add(SysUser sysUser, String roleIds) {
        userService.addSysUser(sysUser, roleIds);
        return resultSucess(null);
    }

    @ResponseBody
    @RequestMapping(value = "/update")
    @Action(operType = OperLogConstant.USER_EDIT, desc = "编辑账号", needLog = true)
    public Map<String, Object> update(SysUser sysUser, String roleIds) {
        userService.updateSysUser(sysUser, roleIds);
        return resultSucess(null);
    }

    @RequestMapping(value = "/detail")
    @Action(desc = "账号详情")
    public ModelAndView detail(Integer uid, String editFlag) {
        ModelAndView result = new ModelAndView("system/user_detail");
        SysUser user = userService.selectByPrimaryKey(uid);
        List<SysRole> roleList = roleService.getAll();
        List<SysRole> hasRoleList = roleService.getRoleByUid(uid);
        for (int i = 0; i < roleList.size(); i++) {
            SysRole c1 = roleList.get(i);
            for (int j = 0; j < hasRoleList.size(); j++) {
                SysRole c12 = hasRoleList.get(j);
                if (c1.getRoleId().equals(c12.getRoleId())) {
                    roleList.remove(i);
                }
            }
        }
        result.addObject("roleList", roleList);
        result.addObject("hasRoleList", hasRoleList);
        result.addObject("user", user);
        result.addObject("editFlag", editFlag);
        return result;
    }
}
